import { useState, useEffect } from 'react'
import Taro, { useLoad, useDidShow } from '@tarojs/taro'
import { ITab } from '@/components/index'
import { View } from '@tarojs/components'
// import { gradeList } from '@/axios/school'
import { getSchoolSubjectList } from '@/axios/common'
import AOVIndex from './components/AOV/index'
import ELPIndex from './components/ELP/index'
import MLPIndex from './components/MLP/index'
import MIIndex from './components/MI/index'
import SIIndex from './components/SI/index'
import OIIndex from './components/OI/index'
import PTIndex from './components/PT/index'

const SchoolMgtIndex: React.FC = () => {
  const [currentTab, setCurrentTab] = useState<number>(0)
  // const [gradeData, setGradeData] = useState<API.GradeItem[]>([])
  const [tabsData, setTabsData] = useState<any[]>([])
  useLoad(() => {})

  const tabsMap = [
    {
      label: '测评概览',
      value: 0,
      id: 0,
      components: <AOVIndex />
    },
    {
      label: '英语能力情况',
      value: 1,
      id: 5,
      components: <ELPIndex />
      // components: <ELPIndex gradeData={gradeData} />
    },
    {
      label: '数学能力情况',
      value: 2,
      id: 6,
      components: <MLPIndex />
      // components: <MLPIndex gradeData={gradeData} />
    },
    {
      label: '性格类型情况',
      value: 3,
      id: 1,
      components: <PTIndex />
      // components: <PTIndex gradeData={gradeData} />
    },
    {
      label: '多元智能',
      value: 4,
      id: 4,
      components: <MIIndex />
    },
    {
      label: '学科兴趣',
      value: 5,
      id: 2,
      components: <SIIndex />
    },
    {
      label: '职业兴趣',
      value: 6,
      id: 3,
      components: <OIIndex />
    }
  ]

  const getSchoolSubjectData = async () => {
    try {
      const results = await getSchoolSubjectList()
      const ids: any[] = results.map(item => item.subjectId)
      const result: any[] = []
      tabsMap.map(item => {
        if (ids.includes(item.id) || item.id == 0) result.push(item)
      })
      setTabsData(result)
    } catch {}
  }

  useDidShow(() => {
    Taro.hideHomeButton()
    // 后续根据查询到的学校 功能列表 过滤 tabsMap
    getSchoolSubjectData()
  })

  // const getGradeData = async () => {
  //   try {
  //     const results = await gradeList()
  //     setGradeData(results || [])
  //   } catch {}
  // }

  // useEffect(() => {
  //   // 查询学校已有年级
  //   getGradeData()
  // }, [])

  return (
    <View className='index tab_bar_page'>
      <ITab
        swipe
        tabData={tabsData}
        currentTab={currentTab}
        onChangeTab={(value: number) => setCurrentTab(value)}
      />
    </View>
  )
}

export default SchoolMgtIndex
